const express = require('express')
const router = express.Router()
const connection = require('../mysql')


//!  添加商品到购物车
router.post('/addCar', (req, res) => {
    // console.log(req.body.params);


    const goodsId = req.body.params.g_id
    const userName = req.body.params.uname


    const sqlTest = "select * from goods_cart where g_id=? and username=?"
    connection.query(sqlTest, [goodsId, userName], (err, result) => {
        if (err) res.send.status(400).json({ msg: '服务器错误' })

        console.log(result);
        

        if (result.length > 0) {
            return res.send({
                status: 201,
                msg: '已经添加过该商品'
            })
        } else {
            //! 先查商品表，获取商品信息，再存入购物车表
            const sql = 'select * from goodslist where g_id =?'
            connection.query(sql, [goodsId], (err, result) => {
                if (err) res.send.status(400).json({ msg: '服务器错误' })

                const { g_name, g_content, g_price, imgUrl } = result[0]
                const g_num = 1

                const sql2 = 'insert into goods_cart ( g_id, userName, g_name, g_content, g_price, g_num,   g_pic ) values (?,?,?,?,?,?,?)'
                connection.query(sql2, [goodsId, userName, g_name, g_content, g_price, g_num, imgUrl], (err, result) => {

                    try {
                        if (err) {

                            return res.send({
                                status: 500,
                                msg: '已经添加过该商品'
                            })
                        }

                        if (result.affectedRows === 1) {
                            return res.send({
                                status: 200,
                                msg: '添加成功'
                            })
                        }
                    } catch (err) {
                        // console.log(err);
                        res.send({
                            status: 404,
                            msg: err
                        })
                    }
                })

            })
        }
    })




})


//!  获取购物车列表
router.get('/getCarDetail', (req, res) => {
    const userName = req.query.userName

    const sql = 'select * from goods_cart where userName =?'

    connection.query(sql, [userName], (err, result) => {
        if (err) res.send.status(400).json({ msg: '服务器错误' })

        if (result.length > 0) {
            res.send({
                status: 200,
                msg: "获取成功购物车列表成功",
                data: result
            })
        } else {
            res.send({
                status: 201,
                msg: "购物车为空"
            })
        }
    })
})


// 删除购物车单个商品
router.post('/delCarItem', (req, res) => {

    // console.log(req.body.params)
    const id = req.body.params.id

    const sql = 'delete from goods_cart where id = ?'

    connection.query(sql, [id], (err, result) => {
        if (err) res.send.status(400).json({ msg: '服务器错误' })


        if (result.affectedRows === 1) {
            res.send({
                status: 200,
                msg: "删除成功"
            })
        } else {
            res.send({
                status: 201,
                msg: "删除失败"
            })
        }
    })

})


//! 修改购物车单个商品数量
router.post('/updateItemNum', (req, res) => {
    // console.log(req.body.params)
    const { id, num } = req.body.params

    const sql = 'update goods_cart set g_num = ? where id = ?'

    connection.query(sql, [num, id], (err, result) => {
        if (err) res.send.status(400).json({ msg: '服务器错误' })
        if (result.affectedRows === 1) {
            res.send({
                status: 200,
                msg: "修改成功"
            })
        } else {
            res.send({
                status: 201,
                msg: "修改失败"
            })
        }
    })
})


module.exports = router